layui.use(['layer','upload','layedit','treeSelect','table','form','laytpl','element'], function(){
    var $ = layui.$
        ,layer = layui.layer
        ,admin = layui.admin
        ,table = layui.table
        ,element = layui.element
        ,form = layui.form
        ,laytpl = layui.laytpl;
    var delFlag = null;
    var treeSelect= layui.treeSelect;//树形下拉框
    var upload = layui.upload;
    var layedit = layui.layedit;

    //回显数据
    //回显数据
    var request = GetRequest();
    var categoryId = null;
    $.get("/content/detail/" + request.id,function (data) {
        $("#id").val(data.id);
        $("#title").val(data.title);
        $("#subTitle").val(data.subTitle);
        $("#tree-select").val(data.categoryId);
        $("#url").val(data.url);
        categoryId = data.categoryId;
        //是否展示
        /*if (data.isShow == '1') {
            $("#isShow").prop("checked","checked");
        } else {
            $("#isShow").prop("checked",false);
        }
        form.render('checkbox');
        form.on('switch(switchTest)', function(data){
            if(this.checked) {
                $("#isShow").val('1');
                data.value="1";
            }else {
                $("#isShow").val('0');
                data.value="0";
            }
            layer.tips('温馨提示：开启后分类会在前台显示，关闭则不会在前台显示', data.othis)
        });*/
        //图片
        $("#pic").val(data.pic);
        if (data.pic != null && data.pic != "") {
            $('#demo2').append('<img width="30%" src="'+ data.pic +'" class="layui-upload-img">');
        }
        $("#layedit_content").html(data.content);

        //创建一个编辑器
        window.editIndex = layedit.build('layedit_content',{
            height : 535,
            uploadImage : {
                url : "/upload/layedit/image"
            }
        });

        layedit.setContent(editIndex,data.content);



    });

    //图片上传
    //图片上传
    upload.render({
        elem: '#test2',
        url: '/upload/pic',
        field: "uploadFile",
        before: function(obj){
        },
        done: function(res){
            console.log(res)
            //上传完毕
            var url = res.url;
            $('#demo2').html('');
            $("#pic").val('');
            $('#demo2').append('<img style="padding: 10px" width="30%" src="'+ url +'" class="layui-upload-img">');
            $("#pic").val(url);
        }
    });

    //树形下拉分类
    treeSelect.render({
        elem: '#tree-select',
        data: '/contentCatogory/tree/select',
        type: 'get',
        placeholder: '请选择所属分类',// 占位符
        search: true,// 是否开启搜索功能：true/false，默认false
        click: function(data){
            $("#tree-select").val(data.current.id);
        },
        success: function (data) { // 加载完成后的回调函数
            $("#tree-select").val(categoryId);
            treeSelect.checkNode('tree', categoryId);
        }
    });
    /*form.on('switch(switchTest)', function(data){
        if(this.checked) {
            $("#isShow").val('1');
            data.value="1";
        }else {
            $("#isShow").val('0');
            data.value="0";
        }
        layer.tips('温馨提示：开启后分类会在前台显示，关闭则不会在前台显示', data.othis)
    });*/

    form.verify({
        content: function(value) {
            return layedit.sync(editIndex);
        }
    });


    //修改产品分类信息
    form.on('submit(edit)', function(data){
        if (data.field.isShow == null || data.field.isShow === "") {
            data.field.isShow = "0";
        }
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.ajax({
            url: '/content/edit',
            data:data.field,
            type: 'post',
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("修改成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    top.layer.close(index);
                    top.layer.msg("修改失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("修改失败！");
            }
        });
        return false;
    });

    //获取路径中的参数
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

});

